package com.turing.post.report.impl;

import java.math.BigInteger;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.report.util.BinaryFileInfo;
import com.turing.post.report.util.BinaryFileInfoDao;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

/**
 * 文件资料表（BinaryFileInfo）实现
 * 
 * @author zxp
 * @Dec 22, 2012
 */
@Stateless(mappedName = "report.impl.BinaryFileInfoDaoImpl")
@Remote
@Local
public class BinaryFileInfoDaoImpl implements BinaryFileInfoDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public BinaryFileInfo getBinaryFileInfoById(BigInteger id) {
		return _em.find(BinaryFileInfo.class, id);
	}

	@Override
	public BigInteger getUniqueBinaryFileKey() {
		String jpql = "SELECT PCPSBINARYFILESEQUENCE.NEXTVAL FROM DUAL";
		Query query = _em.createNativeQuery(jpql);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insertBinaryFileInfo(BinaryFileInfo binaryFileInfo)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, binaryFileInfo);
	}

	@Override
	public void updateBinaryFileInfo(BinaryFileInfo binaryFileInfo)
			throws PCPSApplicationException {
		GeneralDao.update(_em, binaryFileInfo);
	}

	@Override
	public void deleteBinaryFileInfoById(BigInteger id)
			throws PCPSApplicationException {
		GeneralDao.remove(_em, getBinaryFileInfoById(id));
	}

	@Override
	public BigInteger insertOrUpdateBinaryFile(byte[] image,
			BigInteger oldImageId, String fileType)
			throws PCPSApplicationException {
		BigInteger imageId = BigInteger.ZERO;
		// 修改图片信息
		if (image != null && image.length > 0) {
			// 有修改，无创建
			if (oldImageId != null) {
				BinaryFileInfo file = getBinaryFileInfoById(oldImageId);
				if (file != null) {
					file.setFileLength(image.length);
					file.setFileContent(image);
					file.setFileType(fileType);
					updateBinaryFileInfo(file);
				}
				imageId = oldImageId;
			} else {
				imageId = getUniqueBinaryFileKey();
				BinaryFileInfo file = new BinaryFileInfo();
				file.setFileId(imageId);
				file.setFileType(fileType);
				file.setFileLength(image.length);
				file.setFileContent(image);
				insertBinaryFileInfo(file);
			}
		}
		return imageId;
	}
	/**
	 * 新增或创建图片信息--风险
	 * 
	 * @param image
	 * @param oldImageId
	 *            原图片编号
	 * @return
	 * @throws PCPSAppllicationException
	 */
	public void insertXmlFile(BinaryFileInfo binaryFileInfo) throws PCPSApplicationException{
		BigInteger imageId = BigInteger.ZERO;

		if (binaryFileInfo != null) {
			imageId = getUniqueBinaryFileKey();			
			binaryFileInfo.setFileId(imageId);			
			insertBinaryFileInfo(binaryFileInfo);
		}
	}
}